#include "Game.h"

CAsset* SND_Intro; // Temp hack to test sound assets.

GameManager::GameManager(void)
{
	// Do nothing
	m_nWidth = 1024;
	m_nHeight = 768;
	m_bWindowed = true;
    mAssetManager = new CAssetManager();
}

GameManager::~GameManager(void)
{
	// Do nothing
}

void GameManager::startUp(HWND hwnd, HINSTANCE hInst)
{
	// Store some windows variables within this class
	m_hWnd = hwnd;
	m_hInstance = hInst;

	////////////////////////////////////////////////////////////////////////////
	//// Initialize debugging support systems
	////////////////////////////////////////////////////////////////////////////
	m_Logger = CLogger::GetInstance();
	m_Logger->Init();
	m_Logger->OutputErrorConsole("Game Manager Initialized!");


	// Start Timer class
	m_Timer.Start();
	//////////////////////////////////////////////////////////////////////////
	// Initialize rendering subsystems	
	//////////////////////////////////////////////////////////////////////////

	// Specify Direct3D Implementation (D3D9, D3D10, D3D11)
	m_RenderSelection = D3D9;

	// For specific Direct3D Implementations
	switch(m_RenderSelection)
	{
	case D3D9:
		{
			m_Renderer = new CD3D9Renderer;
			m_FontManager = new CD3D9Font;
			m_Sprites = CSpriteManager::GetInstance();

			CD3D9Renderer* m_Renderer9 = dynamic_cast<CD3D9Renderer*>(m_Renderer);
			m_Renderer9->Init(m_hWnd, m_bWindowed, m_nWidth, m_nHeight);

			m_FontManager9 = dynamic_cast<CD3D9Font*>(m_FontManager);
			m_FontManager9->Init(m_Renderer9->GetDevice());

			m_Sprites->Init(m_Renderer9->GetDevice());
			break;
		}
	//case D3D10:
	//	{
	//		m_Renderer = new CD3D10Renderer;
	//		m_FontManager = new CD3D10Font;

	//		CD3D10Renderer* m_Renderer10 = dynamic_cast<CD3D10Renderer*>(m_Renderer);
	//		m_Renderer10->Init(m_hWnd, m_bWindowed, m_nWidth, m_nHeight);

	//		m_FontManager10 = dynamic_cast<CD3D10Font*>(m_FontManager);
	//		m_FontManager10->Init(m_Renderer10->GetDevice());
	//		break;
	//	}
	}

	// Load fonts
	m_FontManager->LoadFont(L"Arial", 20, 0, true);
	m_FontManager->LoadFont(L"Verdana", 20, 0, true);
	m_FontManager->LoadFont(L"Times New Roman", 20, 0, true);

	// WOO! sprite hacks!
	m_Sprites->LoadSprite(L"../sprites/bonus.png");
	m_Sprites->SetSpritePosition(0, D3DXVECTOR3(10,10,-10));
    // Register any assets we need
    SND_Intro = new CSound("../assets/audio/intro.mp3");
    mAssetManager->RegisterAsset(SND_Intro);

    // Load all registered assets
    mAssetManager->LoadAssets(0);
}

void GameManager::run(void)
{
	//////////////////////////////////////////////////////////////////////////
	// Debugging and Performance Info
	//////////////////////////////////////////////////////////////////////////
	// Update timer, get elapsed time since last frame as well as FPS
	float FrameTime = 0;
	int FPS = 0;
	m_Timer.Update();
	FPS = (int)m_Timer.GetFPS();
	FrameTime = m_Timer.GetElapsedTime();

    static bool sound_playing = false;

    if (GetAsyncKeyState(VK_BACK) && true == sound_playing)
    {
        SND_Intro->Stop();
        sound_playing = false;
    }

    if (GetAsyncKeyState(VK_UP) && false == sound_playing)
    {
        SND_Intro->Play();
        sound_playing = true;
    }

	// Check m_Renderer's type using typeid
	string classIDTemp = typeid(*m_Renderer).name();
	wstring className(classIDTemp.begin(), classIDTemp.end());

	// swprintf_s to format the string to display frame time in ms, and FPS
	wchar_t buffer[128];
	swprintf_s(buffer, 128, L"Frame Time: %f ms\nFPS: %d", FrameTime, FPS);
	wstring output = buffer;
	//////////////////////////////////////////////////////////////////////////

	m_Renderer->PreRender();
	m_Renderer->Render();
	m_FontManager->RenderText(0, output, 0, 0, m_nWidth, m_nHeight, DT_LEFT | DT_TOP, D3DXCOLOR(.8f, .8f, .8f, 1.0f));
	m_FontManager->RenderText(1, output, 0, 100, m_nWidth, m_nHeight, DT_LEFT | DT_TOP, D3DXCOLOR(.8f, .8f, .8f, 1.0f));
	m_FontManager->RenderText(2, output, 0, 200, m_nWidth, m_nHeight, DT_LEFT | DT_TOP, D3DXCOLOR(.8f, .8f, .8f, 1.0f));
	m_FontManager->RenderText(0, className, 0, 0, m_nWidth, m_nHeight, DT_RIGHT | DT_TOP, D3DXCOLOR(1.0f, .8f, .8f, 1.0f));

	m_Renderer->PostRender();
}

void GameManager::shutDown(void)
{
	// Shut down in reverse order as startUp()
	m_FontManager->Shutdown();
	m_Renderer->Shutdown();

	m_Logger->Shutdown();
	m_Logger->DeleteInstance();

}